﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.TextUtils;
using HtmlAgilityPack;
using System;
using System.Linq;

namespace CatUtils.Utils.SpecialUtils.Html
{
    /// <summary>
    /// 处理html解码和加密
    /// </summary>
    public partial class HtmlHelper
    {   /// <summary>
        /// Html解码
        /// </summary>
        /// <param name="Html"></param>
        /// <returns></returns>
        public static string Decode(string Html)
        {
            return System.Web.HttpUtility.HtmlDecode(Html);
        }

        /// <summary>
        ///  Html编码
        /// </summary>
        /// <param name="Html"></param>
        /// <returns></returns>
        public static string Encode(string Html)
        {
            return System.Web.HttpUtility.HtmlEncode(Html);
        }

        /// <summary>
        /// 清理lightbox ajax JavaScript 图片lazy load
        /// </summary>
        /// <param name="HtmlCode"></param>
        /// <param name="LazyLoadAttributesName"></param>
        /// <param name="Xpath"></param>
        /// <returns></returns>
        public static string LightBoxClear(string HtmlCode, string LazyLoadAttributesName, string Xpath)
        {
            var HtmlDoc = LoadHtml(HtmlCode);

            HtmlNode[] SelectNode = null;
            if (Xpath != "")
            {
                var htmlNodes = SelectNodes(HtmlDoc, Xpath);
                if (htmlNodes != null)
                    SelectNode = htmlNodes.ToArray();
            }
            else
            {
                PrintLog.E("Htmlhelper.LightBoxClear: Xpath Select Is Null. ");
                return HtmlCode;
            }

            if (SelectNode != null)
            {
                foreach (var Item in SelectNode)
                {
                    try
                    {
                        if (Item.Attributes == null)
                            continue;
                        string IMGSrc = GetAttributesVlaue(Item, LazyLoadAttributesName);
                        Item.Attributes.Remove("src");
                        Item.Attributes.Remove(LazyLoadAttributesName);
                        Item.Attributes.Add("src", IMGSrc);
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                    }
                }
            }
            return InnHtml(HtmlDoc);
        }

        /// <summary>
        /// 替换 AttributesValue的值 可设定通过正则对内容部分替换
        /// </summary>
        /// <param name="HtmlCode"></param>
        /// <param name="AttributesName">替换的 Attributes</param>
        /// <param name="Xpath"></param>
        /// <param name="RepRegex">可设定正则</param>
        /// <param name="RepToValue"></param>
        /// <returns></returns>
        public static string ReplaceAttributesValue(string HtmlCode, string AttributesName, string Xpath, string RepRegex, string RepToValue)
        {
            var HtmlDoc = LoadHtml(HtmlCode);

            HtmlNode[] SelectNode = null;
            if (Xpath != "")
            {
                var htmlNodes = SelectNodes(HtmlDoc, Xpath);
                if (htmlNodes != null)
                    SelectNode = htmlNodes.ToArray();
            }
            else
            {
                PrintLog.E("Htmlhelper.AttributesValueReplace: Xpath Select Is Null. ");
                return HtmlCode;
            }

            if (SelectNode != null)
            {
                foreach (var Item in SelectNode)
                {
                    try
                    {
                        if (Item.Attributes == null)
                            continue;
                        string AttributesValue = GetAttributesVlaue(Item, AttributesName);
                        Item.Attributes.Remove(AttributesName);
                        Item.Attributes.Add(AttributesName,
                            TextHelper.ReplaceTextEnhance(AttributesValue, RepRegex, RepToValue));
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                    }
                }
            }
            return InnHtml(HtmlDoc);
        }

        /// <summary>
        /// 替换 AttributesValue的值  直接删除旧的替换成新的
        /// </summary>
        /// <param name="HtmlCode"></param>
        /// <param name="AttributesName">替换的 Attributes</param>
        /// <param name="Xpath"></param>

        /// <param name="NewValue"></param>
        /// <returns></returns>
        public static string ReplaceAttributesValue(string HtmlCode, string AttributesName, string Xpath, string NewValue)
        {
            var HtmlDoc = LoadHtml(HtmlCode);

            HtmlNode[] SelectNode = null;
            if (Xpath != "")
            {
                var htmlNodes = SelectNodes(HtmlDoc, Xpath);
                if (htmlNodes != null)
                    SelectNode = htmlNodes.ToArray();
            }
            else
            {
                PrintLog.E("Htmlhelper.AttributesValueReplace: Xpath Select Is Null. ");
                return HtmlCode;
            }

            if (SelectNode != null)
            {
                foreach (var Item in SelectNode)
                {
                    try
                    {
                        if (Item.Attributes == null)
                            continue;

                        Item.Attributes.Remove(AttributesName);
                        Item.Attributes.Add(AttributesName, NewValue);
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                    }
                }
            }
            return InnHtml(HtmlDoc);
        }
    }
}